/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.data;

import java.util.Hashtable;

import org.telosys.common.TelosysException;
import org.telosys.common.dataset.DataSet;
import org.telosys.common.dataset.DataSetProvider;
import org.telosys.common.logger.TelosysClassLogger;

/**
 * Static data providers holder, used to store application scope data <br>
 * Different kinds of data providers can be hold :<br>
 * <li>'DataSetProvider' objects
 * <li>'ListItemProvider' objects
 * <li>'MenuItemProvider' objects 
 * 
 * @author Laurent GUERIN
 *  
 */
public class ApplicationDataHolder
{
    private static final TelosysClassLogger $log       = new TelosysClassLogger(ApplicationDataHolder.class);

    private final static Hashtable<String,Object>   $htAppData = new Hashtable<String,Object>(16);

    //---------------------------------------------------------------------------------------------------
    // "REGISTER" OPERATIONS
    //---------------------------------------------------------------------------------------------------
    /**
     * Register/store an Application DataSet provider with a symbolic name
     * 
     * @param sName :
     *            the symbolic name
     * @param appDataSet :
     *            the Application DataSet instance
     * @throws TelosysException
     */
    public synchronized static void registerDataSetProvider(String sName, DataSetProvider appDataSet) throws TelosysException
    {
        //--- NB : NullPointerException if the key or value is null
        if ( sName != null && appDataSet != null )
        {
            $htAppData.put(sName, appDataSet);            
        }
    }

    //---------------------------------------------------------------------------------------------------
    /**
     * Register/store a ListItem provider with a symbolic name
     * 
     * @param sName :
     *            the symbolic name
     * @param listItemProvider :
     *            the ListItem provider instance
     * @throws TelosysException
     */
    public synchronized static void registerListItemProvider(String sName, ListItemProvider listItemProvider) throws TelosysException
    {
        //--- NB : NullPointerException if the key or value is null
        if ( sName != null && listItemProvider != null )
        {
            $htAppData.put(sName, listItemProvider);
        }
    }
    
    //---------------------------------------------------------------------------------------------------
//    /**
//     * Register/store a MenuItem provider with a symbolic name
//     * 
//     * @param sName :
//     *            the symbolic name
//     * @param menuItemProvider :
//     *            the MenuItem provider instance
//     * @throws TelosysException
//     */
//    public synchronized static void registerMenuItemProvider(String sName, MenuItemProvider menuItemProvider) throws TelosysException
//    {
//        //--- NB : NullPointerException if the key or value is null
//        if ( sName != null && menuItemProvider != null )
//        {
//            $htAppData.put(sName, menuItemProvider);
//        }
//    }    
    
    //---------------------------------------------------------------------------------------------------
    // GET OPERATIONS
    //---------------------------------------------------------------------------------------------------
    
    /**
     * Returns the object registered with the given name
     * 
     * @param sName :
     *            the symbolic name
     * @return the object or null if not found
     */
    public static Object getObject(String sName)
    {
        if ($htAppData != null)
        {
            return $htAppData.get(sName);
        }
        else
        {
            $log.error("get(" + sName + ") : Application data is null (not initialized)");
        }
        return null;
    }

    //---------------------------------------------------------------------------------------------------
    /**
     * Returns the DataSetProvider registered with the given name
     * 
     * @param sName :
     *            the symbolic name
     * @return the ApplicationDataSet instance or null if not found
     */
    public static DataSetProvider getDataSetProvider(String sName)
    {
        if ($htAppData != null)
        {
            Object obj = $htAppData.get(sName);
            if (obj != null)
            {
                if (obj instanceof DataSetProvider)
                {
                    DataSetProvider appDS = (DataSetProvider) obj;
                    return appDS;
                }
                else
                {
                    $log.error("getDataSetProvider(" + sName + ") : Object is not an instance of DataSetProvider !");
                }
            }
        }
        else
        {
            $log.error("getDataSetProvider(" + sName + ") : Application data is null (not initialized)");
        }
        return null;
    }
    
    //---------------------------------------------------------------------------------------------------
    /**
     * Returns the DataSet managed by the DataSetProvider stored with the given name
     * 
     * @param sName :
     *            the symbolic name
     * @return the DataSet instance or null if not found
     */
    public static DataSet getDataSet(String sName)
    {
        DataSetProvider appDS = getDataSetProvider(sName);
        if (appDS != null)
        {
            return appDS.getDataSet();
        }
        return null;
    }
    
    //---------------------------------------------------------------------------------------------------
    /**
     * Returns the "ListItem Provider" registered with the given name
     * 
     * @param sName :
     *            the symbolic name
     * @return the ListItem Provider instance or null if not found
     */
    public static ListItemProvider getListItemProvider(String sName)
    {
        if ($htAppData != null)
        {
            Object obj = $htAppData.get(sName);
            if (obj != null)
            {
                if (obj instanceof ListItemProvider)
                {
                    return (ListItemProvider) obj;
                }
                else
                {
                    $log.error("getListItemProvider(" + sName + ") : Object is not an instance of ListItemProvider !");
                }
            }
        }
        else
        {
            $log.error("getListItemProvider(" + sName + ") : Application data is null (not initialized)");
        }
        return null;
    }

    //---------------------------------------------------------------------------------------------------
//    /**
//     * Returns the "MenuItem Provider" registered with the given name
//     * 
//     * @param sName : 
//     *              the symbolic name
//     * @return the MenuItem Provider instance or null if not found
//     */
//    public static MenuItemProvider getMenuItemProvider(String sName)
//    {
//        if ($htAppData != null)
//        {
//            Object obj = $htAppData.get(sName);
//            if (obj != null)
//            {
//                if (obj instanceof MenuItemProvider)
//                {
//                    return (MenuItemProvider) obj;
//                }
//                else
//                {
//                    $log.error("getMenuItemProvider(" + sName + ") : Object is not an instance of MenuItem !");
//                }
//            }
//        }
//        else
//        {
//            $log.error("getMenuItemProvider(" + sName + ") : Application data is null (not initialized)");
//        }
//        return null;
//    }    
}